﻿using System.Security.Cryptography;
using UserDomain.DomainLayer.Entities;
using UserDomain.DomainLayer.RepositoryInterfaces;
using UserDomain.ServiceLayer;
using UtilityComponent.Helpers;
using Container = GeneralUtilities.Utilities.Unity.Container;
using System;

namespace UserDomain.DomainLayer
{
    public class UserService : IUserService
    {
        public IUserRepository UserRepository { get; set; }

        private readonly SecurityHelper _securityHelper;

        /// <summary>
        /// Initializes a new instance of the <see cref="UserService"/> class.
        /// </summary>
        public UserService()
        {
            UserRepository = Container.Resolve<IUserRepository>();
            _securityHelper = new SecurityHelper();

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UserService"/> class.
        /// </summary>
        /// <param name="userRepository">The user repository.</param>
        /// <param name="securityHelper"></param>
        public UserService(IUserRepository userRepository, SecurityHelper securityHelper)
        {
            _securityHelper = securityHelper;
            UserRepository = userRepository;
        }

        /// <summary>
        /// Registers the user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        public User RegisterUser(User user)
        {
            if (String.IsNullOrEmpty(user.UserName)) throw new ArgumentException("Value cannot be null or empty.", "userName");
            if (String.IsNullOrEmpty(user.Password)) throw new ArgumentException("Value cannot be null or empty.", "password");

            user.Password = _securityHelper.MD5Encrypt(user.Password);
            return UserRepository.RegisterUser(user);
        }

        /// <summary>
        /// Validates the user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        public User ValidateUser(User user)
        {
            if (String.IsNullOrEmpty(user.UserName)) throw new ArgumentException("Value cannot be null or empty.", "userName");
            if (String.IsNullOrEmpty(user.Password)) throw new ArgumentException("Value cannot be null or empty.", "password");

            user.Password = _securityHelper.MD5Encrypt(user.Password);
            return UserRepository.ValidateUser(user);
        }

        /// <summary>
        /// Changes the password.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="oldPassword">The old password.</param>
        /// <param name="newPassword">The new password.</param>
        /// <returns></returns>
        public bool ChangePassword(string userName, string oldPassword, string newPassword)
        {
            return true;
        }

    }
}
